<?php
	
	if(!$user->isLogged()) {
		$errors = array();
		$user = new user();
		$profile = new profile();
		
		if (isset($_REQUEST['submit'])) {
			if($user->validateField("name", $_POST['name'])) {
				
				$user->name = $_POST['name'];
			} else {
				
				$errors['name'] = "Nome non valido!";
			}
			
			if($user->validateField("surname", $_POST['surname'])) {
				
				$user->surname = $_POST['surname'];
			} else {
				
				$errors['surname'] = "Cognome non valido!";
			}
			
			if($user->validateField("phone", $_POST['phone'])) {

				$user->phone = $_POST['phone'];
			} else {
				
				$errors['phone'] = "Numero di telefono non valido!";
			}
			
			if($user->validateField("email", $_POST['email'])) {
				if($user->validateField("emailFree", $_POST['email'])) {
					
					$user->email = $_POST['email'];
				} else {
					
					$errors['email'] = "L'indirizzo email indicato &egrave; gi&egrave; registrato";
				}
			} else {
				
				$errors['email'] = "Email non valida!";
			}
			
			if($_POST['password'] == "") {
				
				$errors['password'] = "Inserisci la password desiderata!";
			} else {
				if($_POST['rpassword'] != $_POST['password'] OR $_POST['rpassword'] == "") {
					
					$errors['password'] = "Le due password non corrispondono!";
				} else {
					
					$user->password = md5($_POST['password']);
				}
			}
			
			if($profile->validateField("address", $_POST['address'])) {
				
				$profile->address = $_POST['address'];
				$user->address = $_POST['address'];
			} else {
				
				$errors['address'] = "L'indirizzo non &egrave; valido!";
			}
			
			if($profile->validateField("city", $_POST['city'])) {
				
				$profile->city_id = $_POST['city'];
				$user->city_id = $_POST['city'];
			} else {
				
				$errors['city'] = "Il cap non &egrave; valido!";
			}
			
			$profile->details = $_POST['details'];				
			$user->activation_id = md5(time());
		}
		
		if(isset($_REQUEST['submit']) && count($errors)==0) {
			$insert_id = $user->toDB();
			
			if(!$insert_id) {
				
				echo "<script>alert('Errore nell\'inserimento dell\'utente')</script>";
			} else {
				$profile->user_id = $insert_id;
				if($profile->toDB()) {

					$message = "Ciao " . $user->name . ' ' . $user->surname . "\n
					per attivare l'account da lei registrato su www.failbook.ch la invitiamo a visitare il seguente collegamento\n
					http://www.failbook.ch/index.php?p=activate&email=" . $user->email . "&activation_id=" . $user->activation_id . "\n
					\n\n
					Se dovesse avere ricevuto questa email per sbaglio la invitiamo a cancellarla.\n
					Lo staff di Pizzaway.";
					
					mail($user->email, "Pizzaway - Conferma account", $message);
					
					$user->login($user->email, $user->password);
					
					header('Location: index.php');
				} else {
					
					echo "<script>alert('Errore nell\'inserimento del profilo')</script>";
				}
			}
		}
?>
	
		<div id="quote">
			<form action="#" method="post">
				<h3>Dati di accesso</h3>
				<table>	
					<tr>		
						<td class="desc">email *</td>		
						<td><input type="text" name="email"	value="<?php echo $user->email; ?>"></td>		
						<td class="error">
							<?php 
								if(isset($errors['email'])) { 
									echo $errors['email']; 
								} 
							?>
						</td>	
					</tr>	
					<tr>		
						<td class="desc">password *</td>		
						<td><input type="password" name="password"></td>		
						<td class="error">
							<?php 
								if(isset($errors['password'])) { 
									echo $errors['password']; 
								} else if(count($errors)>0) { 
									echo "Inserisci nuovamente la password desiderata!"; 
								} 
							?>
						</td>	
					</tr>	
					<tr>		
						<td class="desc">ripeti password *</td>		
						<td><input type="password" name="rpassword"></td>		
						<td class="error"></td>	
					</tr>
				</table>
				<h3>Indirizzo di consegna e fatturazione</h3>
				<table>	
					<tr>		
						<td class="desc">nome *</td>		
						<td><input type="text" name="name" value="<?php echo $user->name; ?>"></td>		
						<td class="error"><?php if(isset($errors['name'])) { echo $errors['name']; } ?></td>	
					</tr>	
					<tr>		
						<td class="desc">cognome *</td>		
						<td><input type="text" name="surname" value="<?php echo $user->surname; ?>"></td>		
						<td class="error"><?php if(isset($errors['surname'])) { echo $errors['surname']; } ?></td>	
					</tr>	
					<tr>		
						<td class="desc">telefono *</td>		
						<td><input type="text" name="phone" value="<?php echo $user->phone; ?>"></td>		
						<td class="error"><?php if(isset($errors['phone'])) { echo $errors['phone']; } ?></td>	
					</tr>	
					<tr>		
						<td colspan="3">&nbsp;</td>	
					</tr>	
					<tr>		
						<td class="desc">indirizzo *</td>		
						<td><input type="text" name="address" value="<?php echo $profile->address; ?>"></td>		
						<td class="error"><?php if(isset($errors['address'])) { echo $errors['address']; } ?></td>	
					</tr>	
					<tr>		
						<td class="desc">localit&agrave; *</td>		
						<td>
							<select name="city">			
							<?php
									$temp = "";
									$cities = mysql_query("SELECT * FROM `city` ORDER BY `name`");
									while($city = mysql_fetch_array($cities)) {
										if($temp != $city['name']) {
											$temp = $city['name'];
											
											if($city['ID'] == $profile->city_id) { 
												echo '<option value="'.$city['ID'].'" selected>';
											} else {
												echo '<option value="'.$city['ID'].'">';
											}
											
											echo $city['name'];
											echo '</option>';
										}
									}
								?>	
								</select>
							</td>		
							<td class="error">
							<?php 
								if(isset($errors['city'])) { 
									echo $errors['city']; 
								} 
							?>
							</td>	
					</tr>	
					<tr>		
						<td class="desc">dettagli indirizzo</td>		
						<td><input type="text" name="details" value="<?php echo $profile->details; ?>"></td>		
						<td class="error">&nbsp;</td>	
					</tr>	
					<tr>		
						<td colspan="3">&nbsp;</td>	
					</tr>	
					<tr>		
						<td class="desc">&nbsp;</td>		
						<td colspan="2"><input type="submit" name="submit" value="Registrati"></td>	
					</tr>
					<tr>		
						<td colspan="3">Tutti i campi segnati con * sono obbligatori!</td>	
					</tr>
				</table>
			</form>
		</div>
<?php
	} else {
		echo 'Impossibile registrarsi, login gi&agrave; effettuato!';
	}
?>
